'''
Created on Nov 10, 2009

@author: mkiyer
'''
from sqlalchemy import Table, Column, Integer, Float, PickleType, String, \
    MetaData, ForeignKey, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Sample(Base):
    '''
    Represents information about a physical tissue/cell line
    '''
    id = Column(Integer, primary_key=True)    
    pass

class Library(Base):
    '''
    Represents information about a cDNA library constructed for 
    sequencing  
    '''
    __tablename__ = 'libraries'
    
    id = Column(Integer, primary_key=True)
    dna_conc = Column(Float, nullable=False)
    rin = Column(Float, nullable=False)
    clusters = Column(Integer, nullable=False)    
    fastq_path = Column(String, nullable=False)
    read_length = Column(Integer, nullable=False)
    protocol = Column(String, nullable=False)
    platform_name = Column(String, nullable=False)
    platform_version = Column(String, nullable=False)
    run_date = Column(String, nullable=False)

    def __init__(self):
        pass
    
    def import_from_sampledb(self):
        '''
        "Column Name","Data Type","Nullable","Data Default","COLUMN ID","Primary Key","COMMENTS"
        "PRE_ANALYSIS_ID","VARCHAR2(50 BYTE)","No","","1","",""
        "RUN_DATE","VARCHAR2(50 BYTE)","No","","2","",""
        "GA","VARCHAR2(50 BYTE)","No","","3","",""
        "FLOWCELL","VARCHAR2(50 BYTE)","No","","4","",""
        "LANE_ID","NUMBER(10,0)","No","","5","",""
        "ANALYSIS_DATE","VARCHAR2(500 BYTE)","No","","6","",""
        "SW_VERSION","VARCHAR2(500 BYTE)","No","","7","",""
        "DATA_PATH","VARCHAR2(2000 BYTE)","No","","8","",""
        "ANALYSIS","VARCHAR2(100 BYTE)","Yes","","9","",""
        "USE_BASE","VARCHAR2(2000 BYTE)","Yes","","10","",""
        "READ_LENGTH","NUMBER(10,0)","Yes","","11","",""
        "SEED_LENGTH","NUMBER(10,0)","Yes","","12","",""
        "MAX_MATCH","NUMBER(10,0)","Yes","","13","",""
        "ELAND_GENOME","VARCHAR2(2000 BYTE)","Yes","","14","",""
        "GENOME_CONTAM","VARCHAR2(2000 BYTE)","Yes","","15","",""
        "GENOME_SPLICE","VARCHAR2(2000 BYTE)","Yes","","16","",""
        "CLUSTER_RAW","NUMBER(10,0)","No","","17","",""
        "CLUSTER_PF","NUMBER(10,0)","No","","18","",""
        "ALIGN_PF","NUMBER(10,0)","No","","19","",""
        "ERR_RATE_PF","NUMBER(10,0)","No","","20","",""
        "CLUSTER_ID","VARCHAR2(50 BYTE)","Yes","","21","",""
        "ENTRY_DATE","DATE","No","","22","",""
        '''
        pass


class ChIPSeqExperiment(object):

    def __init__(self):
        pass
    


class AlignmentReference(Base):
    '''
    Stores information about alignment references
    '''
    __tablename__ = 'alignment_references'


class AlignmentParadigm(Base):
    '''
    Stores parameters used for sequence alignment
    '''
    id = Column(Integer, primary_key=True)    
    executable = Column(String, nullable=False)
    
    
class Quality(Base):
    '''
    Stores quality statistics for a sequencing run 
    '''
    id = Column(Integer, primary_key=True)
    run_id = Column(Integer, nullable=True)    
    total_reads = Column(Integer, nullable=True)
    aligned_reads = Column(Integer, nullable=True)
    mismatch_distribution = Column(String(200), nullable=True)
    gene_read_distribution = Column(String(100), nulltable=True)
    mate_inner_distance_mean = Column(Float, nullable=True)
    mate_inner_distance_stdev = Column(Float, nullable=True)    

    def __init__(self):
        '''
        Constructor
        '''
        pass


class IntervalNode(Base):
    __tablename__ = 'intervals'
    
    id = Column(Integer, primary_key=True)
    priority = Column(Float)
    start = Column(Integer)
    end = Column(Integer)
    minend = Column(Integer)
    maxend = Column(Integer)
    minstart = Column(Integer)    
    value = Column(PickleType, nullable=True)

    cleft = Column(Integer, ForeignKey('intervals.id'), nullable=True)
    cright = Column(Integer, ForeignKey('intervals.id'), nullable=True)
    croot = Column(Integer, ForeignKey('intervals.id'), nullable=True)